package com.avricot.userinfo.browser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.avricot.userinfo.UserInfoUtils;

public enum BrowserName {
    FIREFOX() {
	@Override
	public String getVersion(final String userAgent) {
	    String version;
	    version = UserInfoUtils.getVersionAfterIdentifier(userAgent, "Firefox/");
	    return (version != null) ? version : UserInfoUtils.getVersionAfterIdentifier(userAgent, "FireBird/");
	}
    },
    MSIE() {
	@Override
	public String getVersion(final String userAgent) {
	    if (userAgent.contains("Smartphone") || userAgent.contains("PPC") || userAgent.contains("IEMobile ")) {
		return null;
	    }
	    final String version = UserInfoUtils.getVersionAfterIdentifier(userAgent, "MSIE ");
	    if (version == null) {
		return null;
	    }
	    if (version.equals("7.0") && version.contains("Trident/4.0")) {
		return "8.0 compat. 7.0";
	    }
	    if (version.equals("7.0") && version.contains("Trident/5.0")) {
		return "9.0 compat. 7.0";
	    }
	    return version;
	}
    },
    CHROME() {
	@Override
	public String getVersion(final String userAgent) {
	    if (userAgent.contains("Safari")) {
		return null;
	    }
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "Chrome/");
	}

    },
    OPERA() {
	@Override
	public String getVersion(final String userAgent) {
	    final String version = UserInfoUtils.getVersionAfterIdentifier(userAgent, "Opera/");
	    return version != null ? version : UserInfoUtils.getVersionAfterIdentifier(userAgent, "Opera ");
	}
    },
    ANDROID_WEBKIT {
	@Override
	public boolean isMobileDevice() {
	    return true;
	}

	@Override
	public String getVersion(final String userAgent) {
	    if (userAgent.contains("Android")) {
		return UserInfoUtils.getVersionAfterIdentifier(userAgent, "Version/");
	    }
	    return null;
	}
    },
    MSIE_MOBILE() {
	@Override
	public boolean isMobileDevice() {
	    return true;
	}

	@Override
	public String getVersion(final String userAgent) {
	    if (userAgent.contains("Smartphone") || userAgent.contains("PPC") || userAgent.contains("IEMobile ")) {
		return UserInfoUtils.getVersionAfterIdentifier(userAgent, "MSIE ");
	    }
	    final String version = UserInfoUtils.getVersionAfterIdentifier(userAgent, "MSPIE ");
	    return version != null ? version : UserInfoUtils.getVersionAfterIdentifier(userAgent, "Pocket Internet Explorer/");
	}

    },
    SAFARI_IPHONE() {
	@Override
	public boolean isMobileDevice() {
	    return true;
	}

	@Override
	public String getVersion(final String userAgent) {
	    if (userAgent.contains("iPhone")) {
		return getVersion("Safari/");
	    }
	    return null;
	}
    },
    // TODO: not sure of this one
    DOLPHIN_ANDROID() {
	@Override
	public boolean isMobileDevice() {
	    return true;
	}

	@Override
	public String getVersion(final String userAgent) {
	    return getVersion("Dolphin/");
	}
    },
    SAFARI() {
	@Override
	public String getVersion(final String userAgent) {
	    if (userAgent.contains("Android") || userAgent.contains("Chrome") || userAgent.contains("iPhone")) {
		return null;
	    }
	    return getVersion("Safari/");
	}

    },
    NOKIA() {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "nokia");
	}
    },
    BOT {
	@Override
	public String getVersion(final String userAgent) {
	    final Pattern p = Pattern.compile(UserInfoUtils.botUserAgentRegex);
	    final Matcher m = p.matcher(userAgent);
	    return m.find() ? "" : null;
	}
    },
    SEAMONKEY() {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "SeaMonkey/");
	}
    },
    KONQUEROR() {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "konqueror/");
	}

    },
    OMNIWEB() {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "omniweb/");
	}

    },
    NETSCAPE() {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "Netscape");
	}
    },
    LUNASCAPE() {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "Lunascape");
	}

    },
    AMAYA() {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "amaya");
	}
    },
    UNKOWN() {
	@Override
	public String getVersion(final String userAgent) {
	    return "";
	}
    };

    public boolean isMobileDevice() {
	return false;
    }

    public abstract String getVersion(String userAgent);
}
